﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Configuration;
using System.Xml;
using System.IO;

namespace Comm
{
    public class ConfigSet
    {
        public static void CreateConfig()
        {
            //string path = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "default.config");
            string path = Path.Combine(@"G:\资料\课件及相关资料\项目资料\双向转诊项目\Demo_FileTransfer\Config", "default.config");
            try
            {
                if (!File.Exists(path))
                {
                    XmlDocument document = new XmlDocument();
                    XmlDeclaration xmlDeclare = document.CreateXmlDeclaration("1.0", "utf-8", null);
                    XmlElement node = document.CreateElement("appSettings");
                    document.AppendChild(node);
                    document.Save(path);
                }
            }
            catch (System.Exception)
            {
            	
            }
        }
        public static void SetValue(string key, string value)
        {
            // 获取xml文档
            XmlDocument document = new XmlDocument();
            try
            {
                //string path = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "default.config");
                string path = Path.Combine(@"G:\资料\课件及相关资料\项目资料\双向转诊项目\Demo_FileTransfer\Config", "default.config");
                document.Load(path);
                XmlNode node = document.SelectSingleNode("//appSettings");
                XmlElement element = (XmlElement)node.SelectSingleNode("add[@key='" + key + "']");
                if (element != null)
                    element.SetAttribute("value", value);
                else
                {
                    XmlElement ele = document.CreateElement("add");
                    ele.SetAttribute("key", key);
                    ele.SetAttribute("value", value);
                    node.AppendChild(ele);
                }
                document.Save(path);
            }
            catch (System.Exception)
            {
                //MessageBox.Show("设置配置文件出错！");
            }
        }
        public static string GetValue(string key)
        {
            // 获取xml文档
            XmlDocument document = new XmlDocument();
            try
            {
                //string path = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "default.config");
                string path = Path.Combine(@"G:\资料\课件及相关资料\项目资料\双向转诊项目\Demo_FileTransfer\Config", "default.config");
                document.Load(path);
                XmlNode node = document.SelectSingleNode("//appSettings");
                XmlElement element = (XmlElement)node.SelectSingleNode("add[@key='" + key + "']");
                if (element != null)
                    return element.GetAttribute("value");
            }
            catch (System.Exception)
            {
            }
            return "";
        }

        // 下面的方法只能在独立的exe中调用
        /*public static void SetValue(string key, string value)
        {
            //增加的内容写在appSettings段下 <add key="RegCode" value="0"/>  
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] == null)
            {
                config.AppSettings.Settings.Add(key, value);
            }
            else
            {
                config.AppSettings.Settings[key].Value = value;
            }
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件   
        }

        public static string GetValue(string key)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] == null)
                return "";
            else
                return config.AppSettings.Settings[key].Value;
        } */
    }
}
